import unittest
from api.login import LoginAPI
from api.staff import StaffAPI
from common.common_assert import common_assert
from common.dbutils import DBUtil

class TestStaffAPI(unittest.TestCase):
    TOKEN = None
    ID = None

    @classmethod
    def setUpClass(cls) -> None:
        DBUtil().exe_sql("delete from bs_user where mobile='13298541513'")
    @classmethod
    def tearDownClass(cls) -> None:
        DBUtil().exe_sql("delete from bs_user where mobile='13298541513'")

    def test00_login_succes(self):
        login_data = {"mobile":13800000002,
                      "password":123456}
        response = LoginAPI().login(login_data)
        print(response.json())
        # common_assert(self,response,200,True,10000,"成功")
        common_assert(self,response)
        TestStaffAPI.TOKEN = response.json().get("data")
    def test01_add_user(self):
        add_data = {
            "username": "jack0709t2",
            "mobile": "13298541513",
            "timeOfEntry": "2020-07-09",
            "formOfEmployment": 1,
            "workNumber": "10086",
            "departmentName": "销售",
            "departmentId": "1266699057968001024",
            "correctionTime": "2020-07-30T16:00:00.000Z"
        }
        response = StaffAPI().add_staff(add_data,TestStaffAPI.TOKEN)
        TestStaffAPI.ID = response.json().get("data").get("id")
        print(TestStaffAPI.ID)

        # 调用数据库工具类 查询该记录是否存在对应的表（bs_user）
        result = DBUtil().exe_sql("select * from bs_user where mobile = '13298541513';")
        print(result)
        result_id = result[0][0]
        print(result_id)
        # 断言
        self.assertEqual(TestStaffAPI.ID, result_id)

    def test02_updata_user(self):
        updata_data = {"username":"mike"}
        response = StaffAPI().updata_staff(TestStaffAPI.ID,updata_data,TestStaffAPI.TOKEN)
        print(response.json())
        common_assert(self,response)
    def test03_select_staff(self):
        response = StaffAPI().select_staff(TestStaffAPI.ID,TestStaffAPI.TOKEN)
        print(response.json())
        common_assert(self,response)
    def test04_delete_staff(self):
        response = StaffAPI().delect_staff(TestStaffAPI.ID,TestStaffAPI.TOKEN)
        print(response.json())
        common_assert(self,response)

